package com.ruoyi.common.web.exception;

import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//自定义全局异常处理器
@Slf4j
@ControllerAdvice
public class CustomExptionHander {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public AjaxResult handler(Exception e){

        if (e instanceof BizException){
            BizException bizException = (BizException) e;
            log.error("【业务异常】" + e);
            return AjaxResult.buildCodeAndMsg(bizException.getCode(),bizException.getMsg());
        }else if (e instanceof NullPointerException){
            log.error("【空指针异常】" + e);
            return AjaxResult.error("NULL!");
        }else{
            log.error("【系统异常】" + e);
            return AjaxResult.error(e.getMessage());
        }
    }

}
